import Taro from "@tarojs/taro";
import {Api} from "../api/api";
import {set as setGlobalData} from "./global_data";
import {doSetUser} from "../actions/global";

export const wxActions = {
    getCurrentPageParams, updateCurrentPageParams, showTip, showLoading, showSuccess, autoLogin,
    refreshUserInfo, showSuccessWithBack
}

// 获取当前页面的参数
function getCurrentPageParams(key) {
    return Taro.getCurrentInstance().router.params[key];
}

// 修改当前页面的参数
function updateCurrentPageParams(key, value) {
    const params = Taro.getCurrentInstance().router.params;
    params[key] = value;
    Taro.getCurrentInstance().router.params = params;
}

//无图标提示
function showTip(msg) {
    Taro.showToast({
        title: msg,
        icon: 'none',
        duration: 2000
    })
}

// 显示加载中
function showLoading(msg) {
    Taro.showLoading({
        title: msg,
        mask: true  //防止触摸穿透
    })
    // setTimeout(function () {
    //     Taro.hideLoading()
    // }, 4000)
}

// 提示成功
function showSuccess(msg) {
    Taro.showToast({
        title: msg,
        icon: 'success',
        duration: 2000
    })
}

// 自动登录
export function autoLogin(dispatch, successCallback, failCallback) {
    Taro.login({
        success: function (res) {
            if (res.code) {
                //发起网络请求
                Api.wxLogin({
                    code: res.code
                }).then(res => {
                    setGlobalData("token", res.token);
                    const user = res.user;
                    dispatch(doSetUser({
                        nickName: user.nickName,
                        email: user.email,
                        phonenumber: user.phonenumber,
                        avatar: user.avatar,
                        address: user.address,
                    }));
                    Taro.setStorage({
                        key: "cacheToken",
                        data: res.token
                    });
                    Taro.setStorage({
                        key: "cacheEffectiveTime",
                        data: res.effectiveTime
                    });
                    successCallback && successCallback();
                }).catch((err) => {
                    failCallback && failCallback();
                });
            } else {
                failCallback && failCallback();
                console.log('登录失败！' + res.errMsg)
            }
        },
        fail: function (res) {
            failCallback && failCallback();
        }
    })
}

// 刷新用户信息
function refreshUserInfo(dispatch) {
    // 获取用户账号信息
    Api.getUserDetail().then(res => {
        dispatch(doSetUser({
            nickName: res.nickName,
            email: res.email,
            phonenumber: res.phonenumber,
            avatar: res.avatar,
            address: res.address,
        }));
    });
}


// 提示成功并返回上一级
function showSuccessWithBack(msg) {
    Taro.showToast({
        title: msg,
        icon: 'success',
        duration: 2000
    })
    setTimeout(() => {
        Taro.navigateBack();
    }, 1500)
}


